{{- define "exporter_resources" }}
cpu: 10m
memory: 50Mi
{{- end }}

{{- if (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: terraform-state-exporter
  namespace: d8-system
  {{- include "helm_lib_module_labels" (list . (dict "app" "terraform-state-exporter")) | nindent 2 }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: terraform-state-exporter
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: "exporter"
      minAllowed:
        {{- include "exporter_resources" . | nindent 8 }}
      maxAllowed:
        cpu: 20m
        memory: 100Mi
    {{- include "helm_lib_vpa_kube_rbac_proxy_resources" . | nindent 4 }}
{{- end }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: terraform-state-exporter
  namespace: d8-system
  {{- include "helm_lib_module_labels" (list . (dict "app" "terraform-state-exporter")) | nindent 2 }}
spec:
  {{- include "helm_lib_deployment_strategy_and_replicas_for_ha" . | nindent 2 }}
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      module: {{ $.Chart.Name }}
      app: terraform-state-exporter
  template:
    metadata:
      labels:
        module: {{ $.Chart.Name }}
        app: terraform-state-exporter
    spec:
      {{- include "helm_lib_priority_class" (tuple . "cluster-medium") | nindent 6 }}
      {{- include "helm_lib_node_selector" (tuple . "monitoring") | nindent 6 }}
      {{- include "helm_lib_tolerations" (tuple . "monitoring") | nindent 6 }}
      {{- include "helm_lib_pod_anti_affinity_for_ha" (list . (dict "app" "terraform-state-exporter")) | nindent 6 }}
      {{- include "helm_lib_module_pod_security_context_run_as_user_nobody" . | nindent 6 }}
      imagePullSecrets:
      - name: deckhouse-registry
      serviceAccountName: terraform-state-exporter
      containers:
      - name: exporter
        {{- include "helm_lib_module_pod_security_context_run_as_user_root" . | nindent 8 }}
          allowPrivilegeEscalation: false
          readOnlyRootFilesystem: false
        args:
        - "terraform"
        - "converge-exporter"
        - "--logger-type=json"
        - "--check-interval=10m"
        - "--kube-client-from-cluster"
        image: {{ include "terraform_manager_image" . }}
        livenessProbe:
          httpGet:
            path: /healthz
            scheme: HTTPS
            port: 9100
        readinessProbe:
          httpGet:
            path: /healthz
            scheme: HTTPS
            port: 9100
          timeoutSeconds: 5
        volumeMounts:
          - mountPath: /tmp
            name: tmp
        env:
        - name: DHCTL_CLI_KUBE_CLIENT_FROM_CLUSTER
          value: "true"
        {{- include "helm_lib_envs_for_proxy" . | nindent 8 }}
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_logs_with_extra" 10 | nindent 12 }}
{{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "exporter_resources" . | nindent 12 }}
{{- end }}
      - name: kube-rbac-proxy
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem" . | nindent 8 }}
        image: {{ include "helm_lib_module_common_image" (list . "kubeRbacProxy") }}
        args:
        - "--secure-listen-address=$(KUBE_RBAC_PROXY_LISTEN_ADDRESS):9100"
        - "--v=2"
        - "--logtostderr=true"
        - "--stale-cache-interval=1h30m"
        ports:
        - containerPort: 9100
          name: https-metrics
        env:
        - name: KUBE_RBAC_PROXY_LISTEN_ADDRESS
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: KUBE_RBAC_PROXY_CONFIG
          value: |
            excludePaths:
            - /healthz
            upstreams:
            - upstream: http://127.0.0.1:9101/
              path: /
              authorization:
                resourceAttributes:
                  namespace: d8-system
                  apiGroup: apps
                  apiVersion: v1
                  resource: deployments
                  subresource: prometheus-metrics
                  name: terraform-state-exporter
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_only_logs" . | nindent 12 }}
{{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "helm_lib_container_kube_rbac_proxy_resources" . | nindent 12 }}
{{- end }}
      volumes:
        - name: tmp
          emptyDir: {}
